package com.emapp.lccode.view

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import androidx.viewbinding.ViewBinding
import com.emapp.lccode.adapter.DialogTypeKtAdapter
import com.emapp.lccode.base.dismissLoadingExt
import com.emapp.lccode.bean.ProblemTypeKt
import com.emapp.lccode.bean.SelectType
import com.emapp.lccode.databinding.PopDistrictBinding
import com.emapp.lccode.mode.MapViewModel
import com.emapp.lccode.utils.vertical
import com.hjq.toast.Toaster

/**
 * 类型
 */
class DeviceTypeDialog : BaseDialog1() {
    private var mViewBind: PopDistrictBinding? = null
    private var deTypes1: ArrayList<ProblemTypeKt> = arrayListOf()
    private var deTypes2: ArrayList<ProblemTypeKt> = arrayListOf()
    private var deTypes3: ArrayList<ProblemTypeKt> = arrayListOf()
    private val adapter1: DialogTypeKtAdapter by lazy { DialogTypeKtAdapter() }
    private val adapter2: DialogTypeKtAdapter by lazy { DialogTypeKtAdapter() }
    private val adapter3: DialogTypeKtAdapter by lazy { DialogTypeKtAdapter() }
    private lateinit var myDialogViewModel: MapViewModel
    lateinit var selectType: SelectType
    var pid = 0
    var listener: SelectTypeListener? = null
    var hasInit = false
    fun show(activity: FragmentActivity) {
        show(activity.supportFragmentManager, null)
    }


    override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): ViewBinding {
        return PopDistrictBinding.inflate(inflater, container, false).also { mViewBind = it }
    }

    override fun initView() {
        pid = 0
        myDialogViewModel =
            ViewModelProvider(this)[MapViewModel::class.java]
        mViewBind?.rvList0?.run {
            vertical()
            adapter = adapter1
        }
        adapter1.run {
            setOnItemClickListener { adapter, _, position ->
                val item = items[position]
                //   if (item.isCheck) return@setOnItemClickListener
                items.forEachIndexed { index, problemTypeKt ->
                    problemTypeKt.isCheck = index == position
                }
                notifyDataSetChanged()
                selectType.id0 = item.id
                selectType.name0 = item.title
                selectType.id1 = ""
                selectType.name1 = ""
                selectType.id2 = ""
                selectType.name2 = ""
                deTypes2.clear()
                adapter2.submitList(deTypes2)
                adapter2.notifyDataSetChanged()
                deTypes3.clear()
                adapter3.submitList(deTypes3)
                adapter3.notifyDataSetChanged()
                pid = 1
                myDialogViewModel.getDeviceType(item.id)
            }
        }
        mViewBind?.rvList1?.run {
            vertical()
            adapter = adapter2
        }
        adapter2.run {
            setOnItemClickListener { adapter, _, position ->
                val item = adapter.items[position]
                // if (item.isCheck) return@setOnItemClickListener
                items.forEachIndexed { index, problemTypeKt ->
                    problemTypeKt.isCheck = index == position
                }
                notifyDataSetChanged()
                selectType.id1 = item.id
                selectType.name1 = item.title
                selectType.id2 = ""
                selectType.name2 = ""
                deTypes3.clear()
                adapter3.submitList(deTypes3)
                adapter3.notifyDataSetChanged()
                pid = 2
                myDialogViewModel.getDeviceType(item.id)
            }
        }
        mViewBind?.rvList2?.run {
            vertical()
            adapter = adapter3
        }
        adapter3.run {
            setOnItemClickListener { adapter, _, position ->
                val item = adapter.items[position]
                if (item.isCheck) return@setOnItemClickListener
                items.forEachIndexed { index, problemTypeKt ->
                    problemTypeKt.isCheck = index == position
                }
                notifyDataSetChanged()
                selectType.id2 = item.id
                selectType.name2 = item.title
            }
        }
        myDialogViewModel.run {
            types.observe(viewLifecycleOwner) { resultState ->
                dismissLoadingExt()
                when (pid) {
                    0 -> {
                        if (selectType.id0.isNotEmpty()) {
                            resultState.forEach { type ->
                                type.isCheck = type.id == selectType.id0
                            }
                        }
                        adapter1.submitList(resultState)
                        adapter1.notifyDataSetChanged()
                    }

                    1 -> {
                        if (selectType.id1.isNotEmpty()) {
                            resultState.forEach { type ->
                                type.isCheck = type.id == selectType.id1
                            }
                        }
                        adapter2.submitList(resultState)
                        adapter2.notifyDataSetChanged()
                    }

                    2 -> {
                        if (selectType.id2.isNotEmpty()) {
                            resultState.forEach { type ->
                                type.isCheck = type.id == selectType.id2
                            }
                        }
                        adapter3.submitList(resultState)
                        adapter3.notifyDataSetChanged()
                    }
                }
            }
        }
        myDialogViewModel.getDeviceType("0")
        mViewBind?.ivCancel?.setOnClickListener {
            dismiss()
        }
        mViewBind?.tvOk?.setOnClickListener {
//            if (selectType.id1.isBlank()&&adapter2.itemCount>0) {
//                Toaster.show("请选择二级分类")
//                return@setOnClickListener
//            }
            listener?.onSelectTypeComplete(selectType)
            dismiss()
        }
    }

    companion object {
        private var d: DeviceTypeDialog? = null
        fun create(l: SelectTypeListener): DeviceTypeDialog? {
            if (d == null) {
                d = DeviceTypeDialog()
            }
            d?.listener = l;
            return d
        }
    }

    interface SelectTypeListener {
        fun onSelectTypeComplete(type: SelectType)
    }

}